<!--雷达图-->
<template>
  <div :id='id' :ref='id' :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 1000

export default {
  name:"radarChart",
  mixins: [resize],
  props: {
    id: {
      type: String,
      default: 'radarChart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    chartData: {
      type: Object,
      default: () => ({
        tooltip: {
          trigger: 'item',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        //右侧工具箱
        toolbox: {
          feature: {
            dataView: { show: true, readOnly: false },
            restore: { show: true },
            saveAsImage: { show: true }
          }
        },
        radar: {
          radius: '66%',
          center: ['50%', '42%'],
          splitNumber: 5,
          splitArea: {
            areaStyle: {
              color: 'rgba(127,95,132,.3)',
              opacity: 0.5,
              shadowBlur: 45,
              shadowColor: 'rgba(0,0,0,.5)',
              shadowOffsetX: 0,
              shadowOffsetY: 15
            }
          },
          name: {
            lineHeight: 12,
            formatter: (labelName, raw) => {
              return (
                  labelName
              );
            },
          },
          indicator: [
            { name: '维度1', max: 10000 },
            { name: '维度2', max: 10000 },
            { name: '维度3', max: 10000 },
            { name: '维度4', max: 10000 },
            { name: '维度5', max: 10000 },
            { name: '维度6', max: 10000 }
          ],
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: ['数据1', '数据2', '数据3']
        },
        series: [
          {
            type: 'radar',
            symbolSize: 5,
            areaStyle: {
              normal: {
                shadowBlur: 13,
                shadowColor: 'rgba(0,0,0,.2)',
                shadowOffsetX: 0,
                shadowOffsetY: 10,
                opacity: 0.5
              }
            },
            data: [
              {
                value: [10*1000, 9*1000, 5*1000, 2*1000, 3*1000, 1*1000],
                name: '数据1'
              },
              {
                value: [2*1000, 6*1000, 4*1000, 8*1000, 6*1000, 3*1000],
                name: '数据2'
              },
              {
                value: [7*1000, 9*1000, 1*1000, 5*1000, 5*1000, 3*1000],
                name: '数据3'
              }
            ],
            animationDuration: 3000
          }]
      })
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions()
      }
    }
  },
  mounted() {

  },
  beforeDestroy() {
    this.destroy()
  },
  methods: {
    /**
     * 初始化图表
     */
    initChart() {
      this.destroy()
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions()
      this.setClick()
    },
    /**
     * 更新图表数据
     */
    setOptions() {
      this.$nextTick(() => {
        this.chart.setOption(this.chartData)
      })
    },
    /**
     * 注册点击事件
     */
    setClick(){
      this.chart.getZr().off("click");
      let that = this
      this.chart.getZr().on("click", function (params) {
        let pointInPixel = [params.offsetX, params.offsetY];
        // 使用containPixel API判断点击位置是否在显示图形区域，下面的例子过滤了绘制图形的网格外的点击事件，好比X、Y轴lable、空白位置等的点击事件。
        if (that.chart.containPixel("grid", pointInPixel)) {
          let xIndex = that.chart.convertFromPixel({ seriesIndex: 0 }, [
            params.offsetX,
            params.offsetY,
          ])[0];
          let op = that.chart.getOption();
          //获取x轴坐标数据
          let xData=op.xAxis[0].data[xIndex]
          //获取所有y轴坐标数据
          let yData=[]
          op.series.forEach(item=>{
            yData.push({
              name:item.name,
              data:item.data[xIndex]
            })
          })
          that.$emit("onclick", xData,yData);
        }
      });
    },
    destroy() {
      if (this.chart) {
        this.chart.dispose()
        this.chart = null
      }
    }
  }
}
</script>
